﻿#_*_ coding: UTF-8 _*_

from core import uicomponents, db, html_utils
from PyQt4 import QtGui, QtCore
import servers.quran.definitions


def __getAyeDetails(aye,cnum):
	db.session.refresh(aye)
	a = ""
	b = ""
	for word in aye.words:
		db.session.refresh(word)
		w = word.word
		for mask in servers.quran.definitions.quranWordViewMasks:
			w = servers.quran.definitions.quranWordMasks[mask](word,w)
		for commenter in servers.quran.definitions.quranWordViewCommenters:
			w = servers.quran.definitions.quranWordCommenters[commenter](word,w)
		for comment in word.comments:
			if  comment.last:
				cnum += 1
				w += "<sup><font color=red>" + str(cnum)  + "</font></sup> "
				b +=  str(cnum) + ":" + html_utils.remove_main_head_tags(comment.comment.fa) + "<br>"
		a += w + " "
	a += "<b>(%d)</b>" % aye.n
	c = aye.translation.fa + "<b>(%d)</b>" % aye.n
	return [a,b,c,cnum]

def CopyQuran():
	a = uicomponents.SharedUi.selectionStartWord.aye
	t = ['','','']
	cnum = 0
	while True:
		t1 = __getAyeDetails(a,cnum)
		t[0] += t1[0]
		t[1] += t1[1]
		t[2] += t1[2]
		cnum = t1[3]

		if a == uicomponents.SharedUi.selectionEndWord.aye:
			break;
		a = a.next()

	uicomponents.SharedUi.clipboard = QtGui.QApplication.clipboard()
	uicomponents.SharedUi.clip = QtCore.QMimeData()#clipboard.mimeData()
	c = u'<div dir=rtl><font face="B Badr" size=8>%s</font><br><font face="B Lotus" size=6>%s</font><br><font face="B Lotus" size=6>يعني: %s</font></div><br><font face="B Lotus" size=6>[%s %d]ص%d' %  (t[0].decode("utf8"),t[1].decode("utf8"),t[2].decode("utf8"),uicomponents.SharedUi.selectionStartWord.aye.sure.name,uicomponents.SharedUi.selectionStartWord.aye.n,uicomponents.SharedUi.selectionStartWord.aye.page.page_number)
	if not uicomponents.SharedUi.selectionStartWord.aye == uicomponents.SharedUi.selectionEndWord.aye :
		c += u'-[%s %d]ص%d' % (uicomponents.SharedUi.selectionEndWord.aye.sure.name,uicomponents.SharedUi.selectionEndWord.aye.n,uicomponents.SharedUi.selectionEndWord.aye.page.page_number)
	c += u'</font>'	
	uicomponents.SharedUi.clip.setHtml(c)
	uicomponents.SharedUi.clipboard.setMimeData(uicomponents.SharedUi.clip)



QuranCopyAction = QtGui.QAction(u'کپي', None)
QuranCopyAction.setShortcut(u'Ctrl+C')
QtCore.QObject.connect(QuranCopyAction, QtCore.SIGNAL('triggered()'), CopyQuran)
uicomponents.SharedUi.wordPopupMenu.addAction(QuranCopyAction)
uicomponents.SharedUi.ayePopupMenu.addAction(QuranCopyAction)
uicomponents.SharedUi.selectionPopupMenu.addAction(QuranCopyAction)

